﻿using MonitorPrism_Practice.Mvvm;
using MonitorPrism_Practice.Services;
using MonitorPrism_Practice.ViewModel;
using MonitorPrism_Practice.Views;
using SCADALight.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MonitorPrism_Practice.ViewModels
{
    public class MainWindowViewModel:BindableBase, IViewLoadedAndUnloadedAware
    {
        #region
        private IRegionManager _regionManager { get; }
        private readonly IPlcService _plcService;
        private readonly InterfaceReciveDataViewModel _interfaceReciveDataViewModel1;
        private readonly InterfacePlotViewModel _interfacePlotViewModel;
        private LogEventService _logEventService { get; }
        #endregion



        private string _title = "热塑铺丝机监控,这个能绑定";
       
        public string Title
        {
            get { return _title; }
            set
            {
                SetProperty(ref _title, value);
            }
        }
        private ViewModelStatus _status = ViewModelStatus.None;
        public ViewModelStatus Status
        {
            get { return _status; }
            set
            {
                if (_status != value)
                {
                    _status = value;
                    RaisePropertyChanged("Status");
                }
            }
        }
        public MainWindowViewModel(IRegionManager regionManager, IPlcService plcService,
            InterfaceReciveDataViewModel interfaceReciveDataViewModel,
            InterfacePlotViewModel interfacePlotViewModel)
        {
            _interfaceReciveDataViewModel1 = interfaceReciveDataViewModel;
            _interfacePlotViewModel = interfacePlotViewModel;
            _regionManager = regionManager;
            _plcService = plcService;
             _logEventService = ContainerLocator.Current.Resolve<LogEventService>();
            _logEventService.AddItem(new LogEventModel() { LogLevel = 2, Message = $"系统启动成功{DateTime.Now:yyyy-MM-dd HH:mm:ss}" });

        }

        MainWindow _mainWindow;

        public async void OnLoaded(object sender, RoutedEventArgs e)
        {
            _mainWindow = sender as MainWindow;
            Status = ViewModelStatus.Loading;

            await ConnectPLCAsync();

            Status = ViewModelStatus.None;
            //Navigation("设备测点管理");
        }

        private async Task ConnectPLCAsync()
        {
            try
            {
                bool success = await _plcService.OpenAsync();

            }
            catch (Exception ex)
            {
                _logEventService.AddItem(new LogEventModel() { LogLevel = 2, Message = $"系统启动成功{DateTime.Now:yyyy-MM-dd HH:mm:ss}" });
            }
        }

        public void OnUnloaded(object sender, RoutedEventArgs e)
        {
            
        }

        public async void OnLoaded(MainWindow view)
        {
            Status = ViewModelStatus.Loading;

            await ConnectPLCAsync();

            Status = ViewModelStatus.None;
        }

        public void OnUnloaded(MainWindow view)
        {
            
        }
    }
    public enum ViewModelStatus
    {
        /// <summary>
        /// ViewModel无状态
        /// </summary>
        None = 0x0,
        /// <summary>
        /// ViewModel正在初始化
        /// </summary>
        Initializing = 0x1,
        /// <summary>
        /// ViewModel初始化完毕
        /// </summary>
        Initialized = 0x2,
        /// <summary>
        /// ViewModel正在加载
        /// </summary>
        Loading = 0x4,
        /// <summary>
        /// ViewModel加载完毕
        /// </summary>
        Loaded = 0x8,
        /// <summary>
        /// ViewModel正在保存
        /// </summary>
        Saving = 0x16,
        /// <summary>
        /// ViewModel保存完毕
        /// </summary>
        Saved = 0x32,
        Querying = 0x64,
        Showed = 0x128,

    }
}
